package com.grj.service.article.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.grj.model.article.entity.Comment;
import com.grj.service.article.service.CommentService;
import common.filter.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.concurrent.*;

/**
 * @author 14505
 */

@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor
public class CommentController {


    private final CommentService commentService;

    private final StringRedisTemplate stringRedisTemplate;


    @GetMapping("/{id}/list")
    public Result<Object> list(@PathVariable String id) {
        return Result.success(commentService.list(new LambdaQueryWrapper<Comment>().eq(Comment::getArticleId,id)));
    }

    @PostMapping("/check/{i}")
    public Result<Object> add(@RequestBody Comment comment, @PathVariable Integer i) {
        comment.setArticleId(comment.getArticleId());
        comment.setCommentId(null);
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        commentService.save(comment);
        stringRedisTemplate.boundHashOps("comment").put(comment.getCommentId().toString(), JSONUtil.toJsonStr(comment));
        ExecutorService executorService = new ThreadPoolExecutor(1, 2, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());
        executorService.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(10);
                int j = 1;
                try{
//                    myFactory.check(i);
                }catch (Exception e){
                    j=0;
                    System.out.println("失败");
                }
                if(j==1){
                    System.out.println("ok");
                    String s = (String) stringRedisTemplate.boundHashOps("comment").get(comment.getCommentId().toString());
                    Comment c = JSONUtil.toBean(s, Comment.class);
                    c.setStatus(1);
                    stringRedisTemplate.boundHashOps("comment").delete(c.getCommentId().toString());
                    stringRedisTemplate.boundListOps("comment::ok").leftPush(JSONUtil.toJsonStr(c));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.shutdown();
        return Result.success("OK");
    }

    @PutMapping("{id}")
    public Result<Object> update(@PathVariable String id) {
        String s = (String) stringRedisTemplate.boundHashOps("comment").get(id);
        Comment comment = JSONUtil.toBean(s, Comment.class);
        comment.setStatus(1);
        stringRedisTemplate.boundHashOps("comment").delete(id);
        stringRedisTemplate.boundListOps("comment::ok").leftPush(JSONUtil.toJsonStr(comment));
        return Result.success("OK");
    }

}
